home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / processes / mp threaded sort / appleeventhandling.cp next >
Encoding:
Text File  |  2000-09-28  |  9.1 KB  |  301 lines

  1. /*
  2.     File:        AppleEventHandling.cp
  3.  
  4.     Contains:    Minimalist support for the required and Display Manager AppleEvents
  5.                 We also support openning and printing “LetterSpec” documents for AOCE.
  6.                 
  7.                 To really support AppleScript™, we’ll need to do ALOT more work in here.
  8.  
  9.     Written by: Dave Falkenburg    
  10.  
  11.     Copyright:    Copyright © 1993-1999 by Apple Computer, Inc., All Rights Reserved.
  12.  
  13.                 You may incorporate this Apple sample source code into your program(s) without
  14.                 restriction. This Apple sample source code has been provided "AS IS" and the
  15.                 responsibility for its operation is yours. You are not permitted to redistribute
  16.                 this Apple sample source code as "Apple sample source code" after having made
  17.                 changes. If you're going to re-distribute the source, we require that you make
  18.                 it clear in the source that the code was descended from Apple sample source
  19.                 code, but that you've made changes.
  20.  
  21.     Change History (most recent first):
  22.                 7/27/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  23.                 11/16/94    DRF                Add StandardAEIdleProc for people who like using AESend with
  24.                                             kAEWaitReply.
  25.                 11/12/94    DRF                Removed extra #include.
  26.                  9/27/94    DRF                 AppLib.h is now Sprocket.h
  27.                   9/4/94    DRF                Added stub Text Services AppleEvent handlers.
  28.  
  29. */
  30.  
  31. #include "Sprocket.h"
  32.  
  33. #include <AppleEvents.h>
  34. #include <Errors.h>
  35. #include <Displays.h>            //    for Display Manager AppleEvent constants
  36. #include <OCEStandardMail.h>    //    for LetterSpec
  37.  
  38. #if    qInlineInputAware
  39. #include <TextServices.h>
  40. #endif
  41.  
  42. #include "AppleEventHandling.h"
  43.  
  44. static pascal Boolean StandardAEIdleProc(EventRecord *theEvent, long *sleepTime, RgnHandle *mouseRgn);
  45.     
  46.  
  47. void
  48. InstallAppleEventHandlers(void)
  49.     {
  50.     //    It’s probably more efficient to use a table to install these handlers…
  51.     
  52.     (void) AEInstallEventHandler(kCoreEventClass,kAEOpenApplication,NewAEEventHandlerProc(HandleOpenApplication),0,false);
  53.     (void) AEInstallEventHandler(kCoreEventClass,kAEOpenDocuments,NewAEEventHandlerProc(HandleOpenDocuments),0,false);
  54.     (void) AEInstallEventHandler(kCoreEventClass,kAEPrintDocuments,NewAEEventHandlerProc(HandlePrintDocuments),0,false);
  55.     (void) AEInstallEventHandler(kCoreEventClass,kAEQuitApplication,NewAEEventHandlerProc(HandleQuitApplication),0,false);
  56.  
  57.     //    regardless of whether or not we have the display manager, go ahead and register an AppleEvent handler
  58.     (void) AEInstallEventHandler(kCoreEventClass,kAESystemConfigNotice,NewAEEventHandlerProc(HandleSystemConfigNotice),0,false);
  59.  
  60. #if    qInlineInputAware
  61.     if (gHasTextServices)
  62.         {
  63.         (void) AEInstallEventHandler(kTextServiceClass,kUpdateActiveInputArea,NewAEEventHandlerProc(HandleTextServicesUpdateActiveInputArea),0,false);
  64.         (void) AEInstallEventHandler(kTextServiceClass,kPos2Offset,NewAEEventHandlerProc(HandleTextServicesPos2Offset),0,false);
  65.         (void) AEInstallEventHandler(kTextServiceClass,kOffset2Pos,NewAEEventHandlerProc(HandleTextServicesOffset2Pos),0,false);
  66.         //    we don’t need to handle <kTextServiceClass,kShowHideInputWindow> events
  67.         }
  68. #endif
  69.     }
  70.  
  71.  
  72. OSErr
  73. CheckAppleEventForMissingParams(AppleEvent *theAppleEvent)
  74.     {
  75.     DescType    returnedType;
  76.     Size        actualSize;
  77.     OSErr        err;
  78.     
  79.     err = AEGetAttributePtr(theAppleEvent,keyMissedKeywordAttr,typeWildCard,
  80.                             &returnedType,nil,0,&actualSize);
  81.     
  82.     if (err == errAEDescNotFound)        //    If we couldn’t find the error attribute
  83.         return noErr;                    //        everything is ok, return noErr
  84.     
  85.     if (err == noErr)                    //    We found an error attribute, so
  86.         return errAEEventNotHandled;    //        tell the client we ignored the event
  87.  
  88.     return err;                            //    Something else happened, return it
  89.     }
  90.  
  91.  
  92. AEIdleUPP        StandardAEIdleUPP = NewAEIdleProc(StandardAEIdleProc);
  93.  
  94. static pascal Boolean
  95. StandardAEIdleProc(EventRecord *theEvent, long * /* sleepTime */, RgnHandle * /* mouseRgn */)
  96.     {
  97.     HandleEvent(theEvent);            //    First, always hand event off to our event handling code
  98.     return false;                    
  99.     }
  100.  
  101.  
  102. OSErr
  103. ForEachDocumentInList(AEDescList documentList,EachDocumentProcPtr documentProc,void * documentParam)
  104.     {
  105.     long                documentCount,documentIndex;
  106.     DescType            returnedType;
  107.     Size                actualSize;
  108.     LetterDescriptor    theLetterDesc;
  109.     AEKeyword            keyword;
  110.     OSErr                err;
  111.  
  112.     if ((err = AECountItems(&documentList,&documentCount)) != noErr)
  113.         return err;
  114.     
  115.     for (documentIndex=1; documentIndex <= documentCount; documentIndex++)
  116.         {
  117.         //    What kind of document is it?
  118.         if ((err = AESizeOfNthItem(&documentList,documentIndex,&returnedType,&actualSize)) != noErr)
  119.             return err;
  120.         
  121.         //    Is it an AOCE letter?
  122.         if (returnedType == typeLetterSpec)
  123.             {
  124.             //    It’s a letter
  125.             theLetterDesc.onDisk = false;
  126.             err = AEGetNthPtr(&documentList,documentIndex,typeLetterSpec,&keyword,&returnedType,
  127.                                 (Ptr) &theLetterDesc.u.mailboxSpec, sizeof(theLetterDesc.u.mailboxSpec),&actualSize);
  128.             }
  129.         else
  130.             {
  131.             //    It’s just a normal document file
  132.             theLetterDesc.onDisk = true;
  133.             err = AEGetNthPtr(&documentList,documentIndex,typeFSS,&keyword,&returnedType,
  134.                                 (Ptr) &theLetterDesc.u.fileSpec,sizeof(theLetterDesc.u.fileSpec),&actualSize);
  135.             }
  136.             
  137.         if (err == noErr)
  138.             (*documentProc)(&theLetterDesc,documentParam);
  139.         else
  140.             break;
  141.         }
  142.     
  143.     return    err;
  144.     }
  145.  
  146.     
  147. pascal OSErr
  148. HandleOpenApplication(AppleEvent *theAppleEvent,AppleEvent * /*reply*/,long /*refCon*/)
  149.     {
  150.     OSErr    err;
  151.     
  152.     if ((err = CheckAppleEventForMissingParams(theAppleEvent)) != noErr)
  153.         return err;
  154.  
  155.     return(CreateNewDocument());
  156.     }
  157.  
  158.  
  159. pascal OSErr
  160. HandleOpenDocuments(AppleEvent *theAppleEvent,AppleEvent * /*reply*/,long /*refCon*/)
  161.     {
  162.     AEDescList            documentList;
  163.     OSErr                err;
  164.     
  165.     if ((err = AEGetParamDesc(theAppleEvent,keyDirectObject,typeAEList,&documentList)) != noErr)
  166.         return err;
  167.  
  168.     if ((err = CheckAppleEventForMissingParams(theAppleEvent)) != noErr)
  169.         return err;
  170.  
  171.     err = ForEachDocumentInList(documentList,&OpenDocument,nil);
  172.     (void) AEDisposeDesc(&documentList);
  173.     return err;
  174.     }
  175.  
  176.  
  177. pascal OSErr
  178. HandlePrintDocuments(AppleEvent *theAppleEvent,AppleEvent * /*reply*/,long /*refCon*/)
  179.     {
  180.     AEDescList            documentList;
  181. #if    qUseQuickDrawGX
  182.     AEDescList            desktopPrinterList;
  183.     FSSpec                thePrinterFSSpec;
  184.     Boolean                draggedToDesktopPrinter = false;
  185. #endif
  186.     OSErr                err;
  187.     
  188.     if ((err = AEGetParamDesc(theAppleEvent,keyDirectObject,typeAEList,&documentList)) != noErr)
  189.         return err;
  190.  
  191. #if    qUseQuickDrawGX
  192.     if (noErr == AEGetAttributeDesc(theAppleEvent,keyOptionalKeywordAttr,typeAEList,&desktopPrinterList))
  193.         draggedToDesktopPrinter = true;
  194. #endif
  195.  
  196.     if ((err = CheckAppleEventForMissingParams(theAppleEvent)) != noErr)
  197.         return err;
  198.  
  199. #if    qUseQuickDrawGX
  200.     if (draggedToDesktopPrinter)
  201.         {
  202.         DescType            returnedType;
  203.         Size                actualSize;
  204.         AEKeyword            keyword;
  205.  
  206.         err = AEGetNthPtr(&desktopPrinterList,1,typeFSS,&keyword,&returnedType,
  207.                             (Ptr) &thePrinterFSSpec,sizeof(thePrinterFSSpec),&actualSize);
  208.  
  209.         (void) AEDisposeDesc(&desktopPrinterList);
  210.         }
  211.     err = ForEachDocumentInList(documentList,&PrintDocument,draggedToDesktopPrinter ? &thePrinterFSSpec : NULL);
  212.     (void) AEDisposeDesc(&documentList);
  213. #else
  214.     err = ForEachDocumentInList(documentList,&PrintDocument,nil);
  215. #endif
  216.  
  217.     return err;
  218.     }
  219.  
  220.  
  221. pascal OSErr
  222. HandleQuitApplication(AppleEvent *theAppleEvent,AppleEvent * /* reply */,long /* refCon */)
  223.     {
  224.     OSErr    err;
  225.     
  226.     if ((err = CheckAppleEventForMissingParams(theAppleEvent)) != noErr)
  227.         return err;
  228.  
  229.     gDone = QuitApplication();
  230.     
  231.     if (gDone)
  232.         return noErr;
  233.     else
  234.         return userCanceledErr;
  235.     }
  236.  
  237.  
  238. ////////////////////////////////////////////////////////////////////////
  239. //
  240. //    Display Manager Suite is “Under Construction”
  241. //
  242. //    To Do: Check ERS for Display Manager events
  243.  
  244. pascal OSErr
  245. HandleSystemConfigNotice(AppleEvent *theAppleEvent,AppleEvent * /* reply */,long /* refCon */)
  246.     {
  247.     AEDescList    displayList;
  248.     long        displayCount,displayIndex;
  249.     OSErr        err;
  250.     
  251.     DebugStr((StringPtr) "\pGot a Display Manager Event!");
  252.     
  253.     if ((err = AEGetParamDesc(theAppleEvent,kAEDisplayNotice,typeAEList,&displayList)) != noErr)
  254.         return err;
  255.     
  256.     if ((err = CheckAppleEventForMissingParams(theAppleEvent)) != noErr)
  257.         return err;
  258.     
  259.     if ((err = AECountItems(&displayList,&displayCount)) != noErr)
  260.         return err;
  261.  
  262.     for (displayIndex = 1; displayIndex<=displayCount; displayIndex++)
  263.         {
  264.         DebugStr((StringPtr) "\pProcessing a display");
  265.         
  266.         //    Gather up all the old and new display rectangles into an oldDeskRegion and a newDeskRegion
  267.         }
  268.  
  269.     //    run through all windows calling keeping all windows which were onscreen in the oldDeskRegion
  270.     //    onscreen in the new world. We should really be calling a method to do this so that windows
  271.     //    can individually deal with things in a manner which they can override.
  272.  
  273.  
  274.     return errAEEventNotHandled;    //    we really don’t handle this yet...
  275.     }
  276.  
  277. #if    qInlineInputAware
  278.  
  279. pascal OSErr
  280. HandleTextServicesUpdateActiveInputArea(AppleEvent *theAppleEvent,AppleEvent *reply,long refCon)
  281.     {
  282.     DebugStr("\pTextServicesUpdateActiveInputArea");
  283.     return errAEEventNotHandled;            //    we really don’t handle this yet...
  284.     }
  285.  
  286. pascal OSErr
  287. HandleTextServicesPos2Offset(AppleEvent *theAppleEvent,AppleEvent *reply,long refCon)
  288.     {
  289.     DebugStr("\pTextServicesPos2Offset");
  290.     return errAEEventNotHandled;            //    we really don’t handle this yet...
  291.     }
  292.     
  293. pascal OSErr
  294. HandleTextServicesOffset2Pos(AppleEvent *theAppleEvent,AppleEvent *reply,long refCon)
  295.     {
  296.     DebugStr("\pTextServicesOffset2Pos");
  297.     return errAEEventNotHandled;            //    we really don’t handle this yet...
  298.     }
  299.  
  300. #endif
  301.